#ifndef TASKITEM_H
#define TASKITEM_H

#include <QObject>
#include <QWidget>

#include <QMap>
#include <QString>
#include <QMouseEvent>
#include <QCheckBox>
#include <QSlider>
#include <QSpinBox>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QComboBox>
#include <QLabel>
#include <QRadioButton>
#include <QMessageBox>

#include <QJsonDocument>    // 解析json
#include <QJsonObject>      // {}
#include <QJsonArray>       // []

#include "algorithmstrategy.h"

class TaskItem : public QWidget
{
    Q_OBJECT
public:
    explicit TaskItem(QWidget *parent = nullptr);
    TaskItem(const TaskItem &taskItem, QWidget *parent = nullptr) = delete;
    TaskItem& operator=(const TaskItem &) = delete;

    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString> &){}
    const QMap<QString,QString>& getParam() const;

    void setTaskName(const QString &name);          // 设置任务名称
    void setClick(bool click);                      // 设置任务状态
    void setAlgorithm(AlgorithmStrategy* algorithm);// 设置算法
    const AlgorithmStrategy* getAlgorithm();        // 获取算法

    void method(Mat& mask, QPixmap* m_tmpPic, QPixmap* m_pic);  // 调用算法
    bool isClicked();
    void setWindowParam(QPixmap* pic, QPixmap* tmpPic, Mat* mask);  // 设置任务窗口的参数

signals:
    void updatePicture();   // 图像处理完，更新Scene里的image
    void updateTask();      // 让线程进行任务处理

public slots:
    void taskStateChange(const bool &state);

public:
    QPixmap* m_pic = nullptr;                       // 指向taskwindow里的
    QPixmap* m_tmpPic = nullptr;                    // 指向taskwindow里的
    Mat *m_mask = nullptr;                          // 指向taskwindow里的

protected:
    QMap<QString, QString> m_param;     //  任务参数
    QVBoxLayout m_verticalLayout;       //  垂直排列
    QCheckBox m_checkbox;               //  勾选框

private:
    AlgorithmStrategy* m_algorithm = nullptr;   // 抽象算法
    // bool m_click = false;
};

// --------------------- 简单工厂 -------------------------
class AbstractFactory{
public:
    static TaskItem *createTaskItem(const QString& name); // 根据 名称不同 创造不同的对象
};

// --------------------- 二值化 -------------------------
class Binarization:public TaskItem
{
public:
    Binarization();

protected:
    QWidget m_widget;
    QHBoxLayout m_layout;
    QLabel m_label;                 // 标签
    QSlider m_horizontalSlider;    // 水平条
    QSpinBox m_spinBox;            // 旋转框
};
class FixeBinarization : public Binarization
{
public:
    FixeBinarization();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);

private:
    QComboBox m_comboBox;
};
class AdaptiveBinarization:public Binarization
{
public:
    AdaptiveBinarization();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);
private:
    QComboBox m_comboBox;
};

// --------------------- 形态学处理 ------------------------
class Morphological:public TaskItem
{
    Q_OBJECT
public:
    Morphological();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);

public slots:
    void radioCheck(bool check);
    void spinboxChange(int kernal);

protected:
    QWidget m_widget;
    QGridLayout m_layout;
    QRadioButton m_radioBtn1;   // 单选框
    QRadioButton m_radioBtn2;   // 单选框
    QLabel m_label;
    QSpinBox m_spinBox;         // 旋转框
    MorphologicalAS* m_mAS = nullptr;
};
class Corrode: public Morphological     // 腐蚀
{
public:
    Corrode();
};
class Swell: public Morphological       // 膨胀
{
public:
    Swell();
};

// --------------------- 滤波 ------------------------
class Filtering: public TaskItem
{
public:
    Filtering();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);
protected:
    QWidget m_widget;
    QHBoxLayout m_layout;
    QLabel m_label;
    QSpinBox m_spinBox;
};
class Guess:public Filtering    // 高斯滤波
{
public:
    Guess();
};
class Median:public Filtering   // 中值滤波
{
public:
    Median();
};
class Mean:public Filtering     // 均值滤波
{
public:
    Mean();
};

// ------------------- 边缘检测 -----------------------
class EdgeDetection: public TaskItem
{
public:
    EdgeDetection();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);
};
class canny: public EdgeDetection       // canny
{
public:
    canny();
};
class sobel: public EdgeDetection       // sobel
{
public:
    sobel();
};
class laplacian: public EdgeDetection   // laplacian
{
public:
    laplacian();
};

// ------------------- 直方图均衡化 -----------------------
class EqualizeHist: public TaskItem
{
public:
    EqualizeHist();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);
};

// ------------------- 彩色图像格式转换 -----------------------
class ColorTo: public TaskItem
{
public:
    ColorTo();
    virtual QJsonArray toJson();
    virtual void setParam(QMap<QString, QString>& param);
};
class RGBtoHSV: public ColorTo
{
public:
    RGBtoHSV();
};
class HSVtoRGB: public ColorTo
{
public:
    HSVtoRGB();
};

#endif // TASKITEM_H
